import { List } from 'immutable';
import { createReducer } from "../utils";
import ACTION_TYPE from '../../constant/action-types';

const initialState = List();
const reducer = {
  [ACTION_TYPE.ADD_DATA_TYPE](state, action) {
    const { payload } = action;
    return state.push(payload);
  },
  [ACTION_TYPE.REMOVE_DATA_TYPE](state, action) {
    const { payload } = action;
    const index = state.indexOf(payload);
    if (index > -1) {
      return state.remove(index);
    }
    return state;
  },
  [ACTION_TYPE.UPDATE_DATA_TYPE](state, action) {
    const { id, data } = action.payload;
    const index = state.findIndex(item => item.id === id);
    if (index > -1) {
      return state.update(index, () => data);
    }
    return state;
  },
};

export default createReducer(reducer, initialState);
